#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/generic/apis/weixin_mp/send_msg_with_tpl.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json
from django import forms
from common.constants import API_TYPE_OP
from components.component import Component, SetupConfMixin
from common.forms import BaseComponentForm, ListField, TypeCheckField
from .toolkit import configs, tools

class SendMsgWithTpl(Component, SetupConfMixin):
    """"""
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_OP

    class Form(BaseComponentForm):
        appid = forms.CharField(label=u'微信业务ID', required=True)
        secret = forms.CharField(label=u'微信业务密码', required=True)
        touser = ListField(label=u'接收者', required=True)
        template_id = forms.CharField(label=u'模版ID', required=True)
        data = TypeCheckField(label=u'消息数据', promise_type=dict, required=False)

    def send_msg(self, wx_client, access_token, data):
        return wx_client.post(path='/cgi-bin/message/template/send?access_token=%s' % access_token, data=json.dumps(data))

    def get_wx_access_token(self, params, need_new_token=False):
        params['need_new_token'] = need_new_token
        wx_token = self.invoke_other('generic.weixin_mp.get_token', kwargs=params)
        return wx_token['data']['access_token']

    def handle(self):
        data = self.request.kwargs
        common_params = {'template_id': data['template_id'],
           'url': data.get('url', ''),
           'data': data['data'],
           'simple_get_token': 1
           }
        wx_client = tools.WEIXINClient(self.outgoing.http_client)
        succ_flag = True
        access_token = self.get_wx_access_token(data)
        for wx_openid in data['touser']:
            common_params['touser'] = wx_openid
            result = self.send_msg(wx_client, access_token, common_params)
            if not result['result'] and result['data'].get('errcode') in (40001, 40014,
                                                                          42001):
                access_token = self.get_wx_access_token(data, need_new_token=True)
                result = self.send_msg(wx_client, access_token, common_params)
            if not result['result']:
                succ_flag = False

        if succ_flag:
            self.response.payload = {'result': True,'message': u'微信消息发送成功。'}
        else:
            self.response.payload = {'result': False,'message': u'微信消息发送失败。'}
